package net.top.viewer.components
{
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;
	
	import mx.managers.IFocusManagerComponent;
	
	import spark.components.Image;
	
	public class FocusableImage extends Image implements IFocusManagerComponent
	{
		public function FocusableImage()
		{
			super();
		}
		
		//--------------------------------------------------------------------------
		//
		//  Focus Management
		//
		//--------------------------------------------------------------------------
		
		private var keyboardPressed:Boolean = false;
		
		override protected function keyDownHandler(event:KeyboardEvent):void
		{
			if (event.keyCode != Keyboard.SPACE)
			{
				return;
			}
			
			keyboardPressed = true;
			event.updateAfterEvent();
		}
		
		override protected function keyUpHandler(event:KeyboardEvent):void
		{
			if (event.keyCode != Keyboard.SPACE)
			{
				return;
			}
			
			if (enabled && keyboardPressed)
			{
				// Mimic mouse click on the button.
				keyboardPressed = false;
				dispatchEvent(new MouseEvent(MouseEvent.CLICK));
			}
			event.updateAfterEvent();
		}
	}
}